/*
 * AlbumBar.fx
 *
 * Created on 15-apr-2009, 16:29:52
 */

package fotoboek.assets;

import fotoboek.Main;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Node;
import javafx.scene.text.Text;
import fotoboek.model.model.Album;

/**
 * @author r.mohammadi
 */

public class AlbumBar {

public-init var skin:Group on replace {
     albumBar_normal = skin.content[1];
 albumBar_clicked = skin.content[1];
 cover = skin.content[2] as ImageView;
 name = skin.content[3] as Text;
 items = skin.content[4] as Text;
 owner = skin.content[5] as Text;
 caption = skin.content[6] as Text;
 textBorder = skin.content[7];
 imageBorder = skin.content[8];
    }
public-init var name:Text;
public-init var caption:Text;
public-init var items:Text;
public-init var owner:Text;
public-init var albumBar_normal:Node;
public-init var albumBar_clicked:Node;
public-init var cover:ImageView;
public-init var textBorder:Node;
public-init var imageBorder:Node;
public-init var album:Album;

public var action: function():Void;

    public var nameText: String on replace {
        if (nameText != null) {
            name.content = nameText;         
        }
    }
    public var captionText: String on replace {
        if (captionText != null) {
            caption.content = captionText;
        }
    }
    public var itemsText: String on replace {
        if (itemsText != null) {
            items.content = itemsText;
        }
    }
    public var ownerText: String on replace {
        if (ownerText != null) {
            owner.content = ownerText;
        }
    }
    public var coverImage:Image on replace{
        if(coverImage != null){
            cover.image = coverImage;
            }
        }

    public var disable = false on replace{       
        albumBar_normal.disable = disable;
        if (disable){
            isPressed = isHovered = false;
            albumBar_clicked.visible = albumBar_normal.visible = false;
        }
        else {
             albumBar_clicked.visible = albumBar_normal.visible = true;
        }
    }    
    public var isPressed: Boolean on replace{
            updateUI();        
    }
    var isHovered: Boolean on replace{        
            updateUI();        
    }
    init{        
        if(albumBar_normal != null){
            albumBar_normal.onMouseEntered = function (e){
                if(not disable){
                    isHovered = true;
                }
            }
            albumBar_normal.onMouseExited = function (e){
                if(not disable){
                    isHovered = false;
                }
            }
            albumBar_normal.onMousePressed = function (e){
                if(not disable){
                    if (Main.isDragging == false){
                        isPressed = true;
                    }
                }
            }
            albumBar_normal.onMouseReleased = function (e){
                if(not disable){
                    isPressed = false;
                    if (Main.isDragging == false){
                        action();
                    }
                }                
            }
        }
        isPressed = false;
        isHovered = false;
        updateUI();
    }

    function updateUI(){
        if (albumBar_clicked != null)
        albumBar_clicked.visible = isPressed;
        if (albumBar_normal != null)
        albumBar_clicked.visible = not isPressed and isHovered;
        if (albumBar_normal != null)
        albumBar_normal.visible = not isPressed and not isHovered;
    }
}
